package com.xinzhi.service.impl;

import com.xinzhi.mapper.ByCardNumberMapper;
import com.xinzhi.pojo.dto.ByCardRequest;
import com.xinzhi.service.IByCardNumberService;
import com.xinzhi.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ByCardNumberServiceImpl implements IByCardNumberService {
    @Autowired
    private ByCardNumberMapper byCardNumberMapper;

    /**
     * 根据卡号查询
     * @param byCardRequest 卡号请求参数
     * @return
     */
    @Override
    public Result getByCardNumber(ByCardRequest byCardRequest) {
        if (byCardRequest == null)
            return Result.error("请补充汇款信息");
        if (!IsCardFormatCorrect(byCardRequest.getCard_number()))
            return Result.error("银行卡号格式错误,请重新输入");
        if (IsBankCardUnique(byCardRequest))
            return Result.error("银行卡号不存在,请重新输入");
        return Result.ok("银行卡号可用");
    }

    /**
     * 校验银行卡号格式
     * @param cardNumber 银行卡号
     * @return
     */
    @Override
    public boolean IsCardFormatCorrect(String cardNumber) {
        return cardNumber.matches("^(\\d{16,19})$");
    }

    /**
     * 校验银行卡号是否唯一
     * @param card_number 银行卡号
     * @return
     */
    @Override
    public boolean IsBankCardUnique(ByCardRequest card_number) {
        return byCardNumberMapper.getByCardNumber(card_number) == null ? true : false;
    }
}
